package cn.appsys.controller;

import cn.appsys.pojo.*;
import cn.appsys.service.backendUser.BackendUserService;
import cn.appsys.service.category.CategoryService;
import cn.appsys.service.dictionary.DictionaryService;
import cn.appsys.service.info.InfoService;
import cn.appsys.service.version.VersionService;
import cn.appsys.tools.Constants;
import cn.appsys.tools.PageSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/backend")
public class BackendUserController {
    @Resource
    BackendUserService backendUserService;
    @Resource
    InfoService infoService;
    @Resource
    VersionService versionService;
    @Resource
    CategoryService categoryService;
    @Resource
    DictionaryService dictionaryService;

    @RequestMapping(value = "/login")
    public String toLogin(){
        return "backendlogin";
    }
    @RequestMapping(value = "/dologin",method = RequestMethod.POST)
    public String doLogin(@RequestParam String userCode,
                          @RequestParam String userPassword,
                          HttpSession session,
                          HttpServletRequest request){
        BackendUser backendUser=null;
        backendUser=backendUserService.getBackendUserByUserCode(userCode,userPassword);
        if(backendUser!=null){
            session.setAttribute(Constants.USER_SESSION,backendUser);
            return "redirect:/backend/flatform/main";
        }else{
            request.setAttribute("error","用户名或密码错误");
            return "backendlogin";
        }
    }
    @RequestMapping(value = "flatform/main")
    public String main(HttpSession session){
        if(session.getAttribute(Constants.USER_SESSION)==null){
            return "redirect:/backend/login";
        }
        return "backendUser/main";
    }
    @RequestMapping(value = "/logOut")
    public String logOut(HttpSession session){
        if(session.getAttribute(Constants.USER_SESSION)!=null){
            session.removeAttribute(Constants.USER_SESSION);
        }
        return "backendlogin";
    }
    @RequestMapping(value = "/applist")
    public String getList(Model model, HttpSession session,
                          @RequestParam(value = "_softwareName", required = false) String _softwareName,
                          @RequestParam(value = "_flatformId", required = false) String _flatformId,
                          @RequestParam(value = "_categoryLevel1", required = false) String _categoryLevel1,
                          @RequestParam(value = "_categoryLevel2", required = false) String _categoryLevel2,
                          @RequestParam(value = "_categoryLevel3", required = false) String _categoryLevel3,
                          @RequestParam(value = "pageIndex", required = false) String pageIndex) {
        List<Info> infoList = null;
        List<Dictionary> statusList = null;
        List<Dictionary> flatformList = null;
        //三级分类
        List<Category> categoryLevel1List = null;
        List<Category> categoryLevel2List = null;
        List<Category> categoryLevel3List = null;
        //分页数据
        int pageSize = Constants.pageSize;
        Integer currentPageNo = 1;
        Integer status = 1;
        Integer categoryLevel1 = null;
        Integer categoryLevel2 = null;
        Integer categoryLevel3 = null;
        Integer flatformId = null;

        if (pageIndex != null) {
            currentPageNo = Integer.valueOf(pageIndex);
        }
        if (_categoryLevel1 != null && !_categoryLevel1.equals("")) {
            categoryLevel1 = Integer.valueOf(_categoryLevel1);
        }
        if (_categoryLevel2 != null && !_categoryLevel2.equals("")) {
            categoryLevel2 = Integer.valueOf(_categoryLevel2);
        }
        if (_categoryLevel3 != null && !_categoryLevel3.equals("")) {
            categoryLevel3 = Integer.valueOf(_categoryLevel3);
        }
        if (_flatformId != null && !_flatformId.equals("")) {
            flatformId = Integer.valueOf(_flatformId);
        }

        int totalCount = 0;
        totalCount = infoService.findAppInfoCount(_softwareName, status, flatformId,
                categoryLevel1, categoryLevel2, categoryLevel3, null);
        PageSupport page = new PageSupport();
        page.setCurrentPageNo(currentPageNo);
        page.setPageSize(pageSize);
        page.setTotalCount(totalCount);
        page.setTotalPageCount(totalCount % pageSize == 0 ?
                (totalCount / pageSize) : (totalCount / pageSize + 1));
        //获取集合
        infoList = infoService.findAppInfoList(_softwareName, status, flatformId,
                categoryLevel1, categoryLevel2, categoryLevel3,
                null, (page.getCurrentPageNo() - 1) * page.getPageSize()
                , page.getPageSize());
        flatformList = this.getDictionaryList("APP_FLATFORM");
        //一级分类
        categoryLevel1List = categoryService.getCategoryList(null);

        model.addAttribute("appInfolist", infoList);
        model.addAttribute("flatformList", flatformList);
        model.addAttribute("categoryLevel1List", categoryLevel1List);
        model.addAttribute("categoryLevel2List", categoryLevel2List);
        model.addAttribute("categoryLevel3List", categoryLevel3List);
        model.addAttribute("pages", page);
        model.addAttribute("softwareName", _softwareName);
        model.addAttribute("categoryLevel1", categoryLevel1);
        model.addAttribute("categoryLevel2", categoryLevel2);
        model.addAttribute("categoryLevel3", categoryLevel3);
        model.addAttribute("flatformId", flatformId);

        return "backendUser/appinfolist";
    }

    public List<Dictionary> getDictionaryList(String typeCode) {
        return dictionaryService.findDictionaryList(typeCode);
    }
    /**
     * 根据parentId查询二三级
     *
     * @param pid
     * @return
     */
    @RequestMapping(value = "/categorylevellist.json", method = RequestMethod.GET)
    @ResponseBody
    public List<Category> getAppCategoryList(@RequestParam String pid) {
        if (pid.equals("")) {
            pid = null;
        }
        return getCategoryList(pid);
    }

    private List<Category> getCategoryList(String pid) {
        List<Category> categoryList = null;
        categoryList = categoryService.getCategoryList(pid == null ? null : Integer.parseInt(pid));
        return categoryList;
    }

    @RequestMapping(value = "/check",method = RequestMethod.GET)
    public String check(@RequestParam(value = "aid",required = false)String aid,
                        @RequestParam(value = "vid",required = false)String vid,
                        Model model){
        Info appInfo=null;
        Version appVersion=null;
        appInfo=infoService.findAppInfo(Integer.parseInt(aid),null);
        appVersion=versionService.getVersionById(Integer.parseInt(vid));
        model.addAttribute(appInfo);
        model.addAttribute(appVersion);
        return "backendUser/appcheck";
    }
    @RequestMapping(value = "checksave",method = RequestMethod.POST)
    public String checkSave(Info info){
        if(infoService.updateStatus(info.getStatus(),info.getId())){
            return "redirect:/backend/applist";
        }
        return "backendUser/appcheck";
    }
}
